﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RayDen.Library.Components.SystemComponents;
using RayDen.RayEngine.Core.Types;

namespace RayDen.RayEngine.Engines.ProgressivePhotonMapping
{
    public class PPMPathIntegrator : PathBuffer<PPMPhotonPath>
    {

        public override void AdvancePaths(RayBuffer rayBuffer)
        {
#if VERBOSE
            try
            {
#endif
            for (int i = firstPath; i != lastPath; i = (i + 1) % paths.Count)
            {
                paths[i].Advance(rayBuffer, this.sampleBuffer);

                if (this.sampleBuffer.IsFull())
                {
                    this.statsTotalSampleCount += this.sampleBuffer.GetSampleCount();
                    // Splat all samples on the film
                    this.film.SplatSampleBuffer(true, sampleBuffer);
                    this.sampleBuffer = this.film.GetFreeSampleBuffer();
                }
            }

            firstPath = (lastPath + 1) % paths.Count;

#if VERBOSE
            }
            catch (Exception ex)
            {
                Tracer.TraceLine(ex.Message);
                Tracer.TraceLine(ex.StackTrace);
                throw;
            }
#if PATH_VERBOSE
            Console.WriteLine("{0} Created Paths / {1} Splatted Paths / {2} Instances currently", createdCount, splattedCount, this.paths.Count);
#endif
#endif
        }
        protected override void OnSplat(PathSamplerBase item)
        {
            base.OnSplat(item);
        }
    }
}
